package 第十周.homework;

import 第八周.practice.Sorting;
import 第十周.practice.Magazine;

import java.util.ArrayList;

/*完成P397页 例题中的节点删除和插入操作，并测试（满分3分，插入删除各1分，测试1分）。
public  void insert(int index, Magazine newMagazine){}//在index的位置插入新节点newMagazine
public void delete(Magazine delNode){}//删除节点delNode
(4)用Magazine类实现Comparable接口，并对链表中的杂志按照字母顺序排序，并输出。
*/
public class MagazineList1 {
    private MagazineNode list;

    //----------------------------------------------------------------
    //  Sets up an initially empty list of magazines.
    //----------------------------------------------------------------
    public MagazineList1() {
        list = null;
    }

    //----------------------------------------------------------------
    //  Creates a new MagazineNode object and adds it to the end of
    //  the linked list.
    //----------------------------------------------------------------
    public void add(Magazine mag) {
        MagazineNode node = new MagazineNode(mag);
        MagazineNode current;

        if (list == null)
            list = node;
        else {
            current = list;
            while (current.next != null)
                current = current.next;
            current.next = node;
        }
    }

    public void insert(int index, Magazine newMagazine) {
        MagazineNode node = new MagazineNode(newMagazine);
        MagazineNode current;
        if (list == null)
            list = node;
        else {
            current = list;
            if (index == 0) {
                node.next = current;
                list = node;
            } else {
                int x = 0;
                while (x < index - 1) {
                    current = current.next;
                    x++;
                }
                node.next = current.next;
                current.next = node;
            }
        }
    }

    public void delete(Magazine delNote) {
        MagazineNode current = list;
        MagazineNode prev = list;
        boolean s = false;
        while (current.next != null) {
            current = current.next;
            if (current.magazine.toString().equals(delNote.toString())) {
                prev.next = current.next;
                s = true;
            } else {
                prev = current;
            }
        }
        if (!s) {
            System.out.println("Not found the knot , the detete failed.");
        }
    }

    public void Sort() {
        int n = 0;
        ArrayList<Comparable> magazines = new ArrayList();
        while (list != null) {
            magazines.add(n, list.getTitle());
            list = list.next;
            n++;
        }
        n = 0;
        Comparable[] list = new Comparable[magazines.size()];
        while (n < magazines.size()) {
            list[n] = magazines.get(n);
            n++;
        }
        Sorting.insertionSort(list);
        for (Comparable a : list)
            System.out.println(a);
    }

    //----------------------------------------------------------------
    //  Returns this list of magazines as a string.
    //----------------------------------------------------------------
    public String toString() {
        String result = "";

        MagazineNode current = list;

        while (current != null) {
            result += current.magazine + "\n";
            current = current.next;
        }

        return result;
    }
}

    //*****************************************************************
    //  An inner class that represents a node in the magazine list.
    //  The public variables are accessed by the MagazineList class.
    //*****************************************************************
     class MagazineNode
    {
        public Magazine magazine;
        public MagazineNode next;

        //--------------------------------------------------------------
        //  Sets up the node
        //--------------------------------------------------------------
        public MagazineNode(Magazine mag)
        {
            magazine = mag;
            next = null;
        }
        public String getTitle() {
            return magazine.getTitle();
        }
    }

